package com.lmk.t04;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DowloadTest {
    public static void main(String[] args) {
        String url="https://img11.360buyimg.com/pop/s590x470_jfs/t1/184121/6/1341/99067/60893c43E327d507a/a0299be04ed70ab2.jpg";
        String fileName=url.substring(url.lastIndexOf("/")+1);
        fileName="d:/11/"+fileName;
        InputStream is=null;
        OutputStream os=null;
        try {
            URL url1=new URL(url);
            HttpURLConnection coon= (HttpURLConnection) url1.openConnection();
            is=coon.getInputStream();

            os=new FileOutputStream(fileName);
            byte[] butter=new byte[500];
            int len=-1;
            while ((len=is.read())!=-1){
                os.write(butter,0,len);

            }
            os.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                if(is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
